-- 我用的
-- --  字库识别。这个api是免费的

local RxOcrUtil = {}

-- 识别
function RxOcrUtil:ocr(x,y,x1,y1, ocrWhite, RxValue, opt)
  mSleep(1)
  opt = opt or 10
  RxValue = RxValue or sysInit.dh.ocrValue.num
  ocrWhite = ocrWhite or sysInit.dh.ocrWhite.oonum
  RxLog('...RxOcrUtil:num 1 - 识别区域文字 start opt='..opt)
  local text = ocrText(x,y,x1,y1, opt, {whitelist=ocrWhite})
  RxLog('...RxOcrUtil:num 2 - 识别结果 - text='..tostring(text))
  text = text or ''
  local lastText = string.gsub(text, "%s+", "")
  RxLog('...RxOcrUtil:num 2 处理空格 lastText='..tostring(lastText))
  mSleep(1)
  return lastText
end

function RxOcrUtil:ocrTable(locTable, ocrWhite, opt)
  local x1 = locTable[1] or 0
  local y1 = locTable[2] or 0
  local x2 = locTable[3] or 0
  local y2 = locTable[4] or 0
  return RxOcrUtil:ocr(x1, y1, x2, y2, ocrWhite, nil, opt)
end

-- 2个字符串 模糊比较相似度，目前不能用于 汉字
function RxOcrUtil:StrsCompare(result, res)
  local ctn,x,y = 0,0,0

  -- 2个字符串中，相同字符串最大长度
  function strCompare(s1, s2)
    s1 = s1 or ''
    s2 = s2 or ''

    local s1Len = RxString:len(s1)
    local s2Len = RxString:len(s2)
    if s1Len == s2Len and s1Len == 1 then
      if s1 == s2 then
        return s1Len
      end
    end

    if s1Len == 0 then
      return 0
    end

    if s1Len <= s2Len and s1Len == 1 then
      local index = RxString:find(s2, s1)
      index = index or 0
      if index >= 0 then
        return 1
      end
    end

    RxLog('...RxOcrUtil:StrsCompare s1='..tostring(s1))
		RxLog('...RxOcrUtil:StrsCompare s2='..tostring(s2))

    RxLog('...RxOcrUtil:StrsCompare s1Len='..tostring(s1Len))
		RxLog('...RxOcrUtil:StrsCompare s2Len='..tostring(s2Len))

    local num = 1
    local sameTable = {}
    local _sameCount = 0
    local space = 2
    for i = 1, s1Len do
      for j = i, s1Len do
        local tempStr = RxString:sub(s1, i, j)
        local index = RxString:find(s2, tempStr)
        local tempLen = RxString:len(tempStr) or -1
        index = index or 0
        RxLog('...RxOcrUtil:StrsCompare 22 strCompare i='..tostring(i)..'-- j='..tostring(j)..' index='..tostring(index)..'--tempLen='..tostring(tempLen)..'-----str='..tostring(tempStr)..'--==== s2='..tostring(s2))
        RxLog('...RxOcrUtil:StrsCompare 22 pro _sameCount='..tostring(_sameCount))
        if index and index > 0 then
          RxLog('...RxOcrUtil:StrsCompare 22 strCompare i= ---- true')
          if _sameCount < tempLen then
            _sameCount = tempLen
          end
        end
      end
    end
    RxLog('...RxOcrUtil:StrsCompare 22 strCompare _sameCount='..tostring(_sameCount))
    return _sameCount
  end

  function delWan(str)
    if not str then return '' end
    local index = string.find(str, '万')
    RxLog('...RxOcrUtil:StrsCompare delWan index='..tostring(index))
    local newStr = str
    if index and index > 1 then
      newStr = string.sub(str, 1, index - 1)
    end
    RxLog('...RxOcrUtil:StrsCompare  delWan newStr='..tostring(newStr))
    return newStr

    -- local strLen = RxString:len(str)
    -- local lastStr = RxString:utf8Sub(str, strLen, strLen)
    -- local newStr = str
    -- if lastStr == '万' then
    --   newStr = RxString:utf8Sub(str, 1, strLen - 1)
    -- end
    -- return newStr
  end
  res = delWan(res)

  local len = RxTable:len(result)
  local num = 0
  local selectNum = 0
  local sameCount = 0
  local haveMultipleSame = 0
  while true do
    num = num + 1
    if num > len then
      break
    end
    local restab = result[num]
    local str = delWan(restab.text)
    local cc = strCompare(str, res)
    RxLog('...RxOcrUtil:StrsCompare 11 cc='..tostring(cc))
    if cc > 0 then
      if selectNum == 0 then
        selectNum = num
        sameCount = cc
      elseif cc > sameCount then
        selectNum = num
        sameCount = cc
        haveMultipleSame = 0
      elseif cc == sameCount then
        haveMultipleSame = 1
      end
    end
  end
  if sameCount > 0 and haveMultipleSame == 0 then
    ctn = 1
    local restab = result[selectNum]
    RxLog('...RxOcrUtil:StrsCompare restab='..tostring(restab))
    x = restab.x or 0
    y = restab.y or 0
    RxLog('...RxOcrUtil:StrsCompare restab x='..tostring(x)..' y='..tostring(y))
  end
  return ctn, x, y
end

return RxOcrUtil